﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Drawing;
using WpfPdfReader.Properties;

namespace WpfPdfReader
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
            this.pdfReaderControl.ViewTypeChanged += new EventHandler<ViewTypeChangedEventArgs>(pdfReaderControl_ViewTypeChanged);
            //恢复窗体位置、大小
            Rect restoreBounds = Properties.Settings.Default.MainRestoreBounds;
            this.Left = restoreBounds.Left;
            this.Top = restoreBounds.Top;
            this.Width = restoreBounds.Width;
            this.Height = restoreBounds.Height;
            //设置窗口状态
            this.WindowState = Properties.Settings.Default.MainWindowState;
            //注册全屏操作按F11
            this.RegisterFullScreen(Key.F11);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.pdfReaderControl.Open(this.PdfFileName);
        }

        private string fileName = "";
        public string PdfFileName
        {
            get { return this.fileName; }
            set
            {
                this.fileName = value;
                //this.pdfReaderControl.FileName = value;
                //this.pdfReaderControl.Open(this.PdfFileName);
            }
        }

        void pdfReaderControl_ViewTypeChanged(object sender, ViewTypeChangedEventArgs e)
        {
            if (e.View == ViewType.ReadView)
                this.EnableKeyDown = true;
            else if (e.View == ViewType.Catalog)
                this.EnableKeyDown = false;
        }

        void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //保存当前位置、大小和状态
            Properties.Settings.Default.MainRestoreBounds = this.RestoreBounds;
            Properties.Settings.Default.MainWindowState = this.WindowState;
            Properties.Settings.Default.Save();
        }

        private void pdfReaderControl_OpenCompleted(object sender, OpenCompletedEventArgs e)
        {
            this.Title = this.pdfReaderControl.FileName;
        }

        private bool EnableKeyDown = false;
        private List<int> InputKeys = new List<int>();

        private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F1)
            {
                if (!this.Topmost)
                {
                    WindowAbout winabout = new WindowAbout();
                    winabout.ShowDialog();
                }
            }
            else if (e.Key == Key.Escape)
            {
                if (MessageBox.Show("是否关闭程序?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                    this.Close();
            }
            else if (e.Key == Key.Space)
            {
                this.pdfReaderControl.GoBack();
            }

            if (this.EnableKeyDown)
            {
                //pdfReaderControl.Focus();

                if (e.Key == Key.PageDown || e.Key == Key.L || e.Key == Key.K || e.Key == Key.S)
                    this.pdfReaderControl.GotoNextPage();
                else if (e.Key == Key.PageUp || e.Key == Key.J || e.Key == Key.I || e.Key == Key.A)
                    this.pdfReaderControl.GotoPreviousPage();
                else if (e.Key == Key.Home)
                    this.pdfReaderControl.GotoFirstPage();
                else if (e.Key == Key.End)
                    this.pdfReaderControl.GotoLastPage();
                else if (e.Key == Key.F || e.Key == Key.OemPlus)
                    this.pdfReaderControl.ZoomIn();
                else if (e.Key == Key.D || e.Key == Key.OemMinus)
                    this.pdfReaderControl.ZoomOut();
                else if (e.Key == Key.Up)
                    this.pdfReaderControl.ScrollToHome();
                else if (e.Key == Key.Down)
                    this.pdfReaderControl.ScrollToBottom();
                else if (e.Key == Key.Right)
                    this.pdfReaderControl.ScrollToRight();
                else if (e.Key == Key.Left)
                    this.pdfReaderControl.ScrollToLeft();
                if (e.Key == Key.B)
                    this.pdfReaderControl.AddBookMark();
                else if (e.Key == Key.M)
                    this.pdfReaderControl.ShowBookManager();
                else
                {
                    int keydata = (int)e.Key;

                    if ((keydata >= 34 && keydata <= 43))
                        this.InputKeys.Add(keydata - 34);
                    else if (keydata >= 74 && keydata <= 83)
                        this.InputKeys.Add(keydata - 73);
                    else if (e.Key == Key.Return)
                        this.InputKeys.Clear();
                    else if (e.Key == Key.G)
                    {
                        string s = "";
                        foreach (int k in InputKeys)
                        {
                            s += k.ToString();
                        }
                        int page;
                        int.TryParse(s, out page);
                        InputKeys.Clear();
                        this.pdfReaderControl.GoToPage(page);
                    }
                }
            }
        }
    }
}
